/*
 * Copyright 2007 Stefan Arentz <http://stefan.arentz.nl>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sateh.spaces.simple;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public class SimpleSpaceObjectStore implements ObjectStore
{
    private final Set<SimpleSpaceObject> objects = new HashSet<SimpleSpaceObject>();
    
    /**
     *
     */

    public void storeObject(SimpleSpaceObject object)
    {
        objects.add(object);
    }
    
    /**
     * @see com.sateh.spaces.simple.ObjectStore#matchAndTakeObject(ObjectMatcher) 
     */

    public SimpleSpaceObject matchAndTakeObject(ObjectMatcher matcher)
    {
        synchronized(objects) {
            for (SimpleSpaceObject simpleSpaceObject : objects) {
                if (!simpleSpaceObject.isExpired()) {
                    if (matcher.willMatch(simpleSpaceObject)) {
                        objects.remove(simpleSpaceObject);
                        return simpleSpaceObject;
                    }
                }
            }
        }

        return null;
    }

    /**
     * @see com.sateh.spaces.simple.ObjectStore#matchAndReadObject(ObjectMatcher) 
     */

    public SimpleSpaceObject matchAndReadObject(ObjectMatcher matcher)
    {
        synchronized(objects) {
            for (SimpleSpaceObject simpleSpaceObject : objects) {
                if (!simpleSpaceObject.isExpired()) {
                    if (matcher.willMatch(simpleSpaceObject)) {
                        return simpleSpaceObject;
                    }
                }
            }
        }

        return null;
    }

    /**
     * @see com.sateh.spaces.simple.ObjectStore#cancelObjectLease(java.util.UUID)  
     */

    public boolean cancelObjectLease(UUID objectId)
    {
        boolean cancelled = false;

        synchronized (objects)
        {
            for (SimpleSpaceObject object : objects) {
                if (!object.isExpired()) {
                    if (object.getId().compareTo(objectId) == 0) {
                        objects.remove(object);
                        cancelled = true;
                        break;
                    }
                }
            }
        }

        return cancelled;        
    }

    /**
     * @see com.sateh.spaces.simple.ObjectStore#renewObjectLease(java.util.UUID, long)
     */

    public boolean renewObjectLease(UUID objectId, long timeToLive)
    {
        synchronized (objects)
        {
            for (SimpleSpaceObject object : objects) {
                if (!object.isExpired()) {
                    if (object.getId().compareTo(objectId) == 0) {
                        object.renew(timeToLive);
                        return true;
                    }
                }
            }
        }

        return false;        
    }

    /**
     * @see com.sateh.spaces.simple.ObjectStore#matchAndCount(ObjectMatcher) 
     */

    public long matchAndCount(ObjectMatcher matcher)
    {
        long count = 0;

        synchronized(objects) {
            for (SimpleSpaceObject simpleSpaceObject : objects) {
                if (!simpleSpaceObject.isExpired()) {
                    if (matcher.willMatch(simpleSpaceObject)) {
                        count++;
                    }
                }
            }
        }        

        return count;
    }
}
